package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Role;
import com.itheima.domain.Result;
import com.itheima.service.RoleService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *  该Servlet处理所有的CRUD操作：
 *  分页查询：http://localhost:8080/heima_mm/system/role/findByPage?currentPage=1&pageSize=5
 *  新增：http://localhost:8080/heima_mm/system/role/save
 *  编辑：http://localhost:8080/heima_mm/system/role/edit
 *  删除：http://localhost:8080/heima_mm/system/role/delete
 */
@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {

    /**
     * 根据用户id查询对应的角色id们
     * @param request
     * @param response
     */
    private Result findRoleIdsByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 获取请求参数 roldId
            String userId = request.getParameter("userId");
            //2 调用service层方法 根据用户id查询对应的角色id们
            List<String> list=roleService.findRoleIdsByUserId(userId);
            //3 响应结果到客户端
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    /**
     * 更新角色绑定的模块，也叫给角色授权
     * @param request
     * @param response
     */
    private Result updateRoleAndModules(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 获取请求参数 roleId moduleIds
            String roleId = request.getParameter("roleId");
            String[] moduleIds = BeanUtil.fillBeanFromJson(request, String[].class);
            //2 调用service，更新角色绑定的模块，也叫给角色授权
            roleService.updateRoleAndModules(roleId,moduleIds);
            //3 响应结果到客户端
            return new Result(true,MessageConstant.SETTING_ROLE_AUTHORIZE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SETTING_ROLE_AUTHORIZE_FAIL);
        }
    }

    /**
     * 查询所有信息的方法
     * @param request
     * @param response
     */
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 调用service，根据查询所有信息
            List<Role> list =roleService.findAll();
            //3 响应结果到客户端
           return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 删除信息的方法
     * @param request
     * @param response
     */
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 获取请求参数并封装
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //2 调用service层方法，批量删除数据
            roleService.delete(ids);
            //3 响应结果
           return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 修改数据的方法
     * @param request
     * @param response
     */
    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 获取请求参数并封装
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            //2 调用service层方法，修改数据
            roleService.update(role);
            //3 响应结果
           return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    /**
     * 根据id查询信息的方法
     * @param request
     * @param response
     */
    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 获取请求参数 id
            String id = request.getParameter("id");
            //2 调用service，根据id查询信息
            Role role=roleService.findById(id);
            //3 响应结果到客户端
           return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 保存信息
     * @param request
     * @param response
     */
    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 获取请求参数并封装
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            //2 调用service层方法，保存数据
            roleService.save(role);
            //3 响应结果
           return new Result(true,MessageConstant.SAVE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.SAVE_ROLE_FAIL);
        }
    }

    /**
     * 处理分页查询的请求
     * @param request
     * @param response
     */
    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RoleService roleService= ac.getBean("roleService", RoleService.class);
        try {
            //1 获取请求参数 currentPage和pageSize
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            //2 非空校验并赋默认值
            int currentPage=1,pageSize=5;
            if(!StringUtils.isBlank(currentPageStr)){
                currentPage=Integer.parseInt(currentPageStr);
            }
            if(!StringUtils.isBlank(pageSizeStr)){
                pageSize=Integer.parseInt(pageSizeStr);
            }

            //3 调用service层方法，执行查询得到pageInfo
            PageInfo<Role> info = roleService.findByPage(currentPage, pageSize);
            //4 响应结果
           return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }
}
